using Microsoft.AspNetCore.Mvc;
using RAG.Application.Services.Interfaces;
using RAG.Domain.Entities.App;
using RAG.Domain.Repositories;
using System.Security.Claims;

namespace RAG.Api.Controllers;

/// <summary>
/// 对话记录控制器
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class ConversationController : ControllerBase
{
    private readonly IConversationService _conversationService;
    private readonly IRepository<Sessions> _sessionRepository;
    private readonly ILogger<ConversationController> _logger;

    public ConversationController(
        IConversationService conversationService, 
        IRepository<Sessions> sessionRepository,
        ILogger<ConversationController> logger)
    {
        _conversationService = conversationService;
        _sessionRepository = sessionRepository;
        _logger = logger;
    }

    /// <summary>
    /// 测试保存对话记录
    /// </summary>
    [HttpPost("test-save")]
    public async Task<ActionResult> TestSaveConversation()
    {
        try
        {
            var userId = Guid.Parse("00000000-0000-0000-0000-000000000001");
            var sessionId = Guid.NewGuid();
            
            // 先创建一个Session记录
            var session = new Sessions
            {
                Id = sessionId,
                SessionName = "测试会话",
                Status = "active",
                SessionType = "chat",
                Description = "测试会话描述",
                LastActiveTime = "刚刚",
                UserId = userId,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = userId,
                UpdatedBy = userId
            };
            
            await _sessionRepository.CreatedAsync(session);
            
            // 然后创建对话记录
            var conversation = new Conversations
            {
                Id = Guid.NewGuid(),
                Question = "测试问题",
                Answer = "测试回答",
                SourceDocuments = "测试文档",
                ConfidenceScore = 0.85m,
                ResponseTimeMs = 1000,
                UserId = userId,
                SessionId = sessionId,
                CreatedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow,
                CreatedBy = userId,
                UpdatedBy = userId
            };

            var savedConversation = await _conversationService.SaveConversationAsync(conversation);
            
            _logger.LogInformation($"对话记录保存成功，ID: {savedConversation.Id}");
            
            return Ok(new { 
                message = "对话记录保存成功", 
                conversationId = savedConversation.Id,
                sessionId = sessionId,
                question = savedConversation.Question,
                answer = savedConversation.Answer
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "保存对话记录时发生错误");
            return StatusCode(500, new { message = "保存对话记录失败", error = ex.Message });
        }
    }
}
